<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Blogs extends Model
{
    // 关联分类表
    public function categories()
    {
        return $this->belongsTo(Categories::class, 'category_id', 'id');
    }

    // 关联用户表
    public function users()
    {
        return $this->hasOne(Users::class, 'id', 'user_id');
    }

    // 关联评论表
    public function comments()
    {
        return $this->hasMany(Comments::class, 'blog_id', 'id');
    }

    // 博客删除时同时删除评论
    public static function onAfterDelete($blog)
    {
        $blog->comments->delete();
    }
}
